# -*- coding: utf-8 -*-
import pymongo

db = pymongo.MongoClient('localhost', 27017).zhihu_sample_database
# del_idp = [i['idp'] for i in db.user_id.find(limit=1000, skip=218001, sort=[("idp", 1)])]
# print(len(del_idp))
# url_token_list = [i['url_token'] for i in db.user_info.find(filter={"idp": {"$in": del_idp}}, projection={'idp': 1, 'url_token': 1, '_id': 0})]
# print(len(url_token_list))
# answer_list = [i['_id'] for i in db.answers.find(filter={"user_token": {"$in": url_token_list}})]
# print(answer_list)
# print(len(answer_list))
# db.answers.delete_many({"_id": {"$in": answer_list}})
#
# db.fail_url_token.insert_one({
#     'url': 'https://www.zhihu.com/api/v4/members/yeka/answers?include=data%5B%2A%5D.is_normal%2Cis_collapsed%2Ccollapse_reason%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Crelationship.is_authorized%2Cvoting%2Cis_author%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B%2A%5D.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics&limit=20&offset=600',
#     'url_token': 'yeka',
#     'error': 'Connection aborted',
#     'user_id': 'ce818bf23f05445cfdf62f79075a6077',
#     'resp': 'not_resp'
# })
#
# db.fail_url_token.insert_one({
#     'url': 'https://www.zhihu.com/api/v4/members/jia-wen-4-76/answers?include=data%5B%2A%5D.is_normal%2Cis_collapsed%2Ccollapse_reason%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Crelationship.is_authorized%2Cvoting%2Cis_author%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B%2A%5D.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics&limit=20&offset=160',
#     'url_token': 'jia-wen-4-76',
#     'error': 'Connection aborted',
#     'user_id': 'd585711af96cae7e6b26d31afcf20f36',
#     'resp': 'not_resp'
# })

tasks = set()
tasks_list = list()
url_token = []
for each_page in db.answers.find(limit=3000, skip=11000, sort=[("_id", 1)]):
    # print(each_page['user_token'])
    # url_token.append(each_page['user_token'])
    for each_answer in each_page['data']:
        answer_id = each_answer['id']
        tasks.add(str(answer_id))
        tasks_list.append(str(answer_id))
# print(tasks)
# print(len(url_token), len(set(url_token)))
tasks = list(tasks)
print(len(tasks), len(tasks_list))
# print(len(del_idp))
# url_token_list = [i['url_token'] for i in db.user_info.find(filter={"idp": {"$in": del_idp}}, projection={'idp': 1, 'url_token': 1, '_id': 0})]
# print(len(url_token_list))
answer_voters_list = list(db['answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))
answer_voters_list = list(db['fail_answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))

db['answer_voters_offset0limit10000'].delete_many({"answer_id": {"$in": tasks}})
db['fail_answer_voters_offset0limit10000'].delete_many({"answer_id": {"$in": tasks}})

answer_voters_list = list(db['answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))
answer_voters_list = list(db['fail_answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))

# print(len(answer_list))
# db.answers.delete_many({"_id": {"$in": answer_list}})


answer_voters_list = list(db['answer_comments_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))
# answer_voters_list = list(db['fail_answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
# print(len(answer_voters_list))
#
db['answer_comments_offset0limit10000'].delete_many({"answer_id": {"$in": tasks}})
# db['fail_answer_voters_offset0limit10000'].delete_many({"answer_id": {"$in": tasks}})
#
answer_voters_list = list(db['answer_comments_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
print(len(answer_voters_list))
# answer_voters_list = list(db['fail_answer_voters_offset0limit10000'].find(filter={"answer_id": {"$in": tasks}}))
# print(len(answer_voters_list))

# print(len(answer_list))
# db.answers.delete_many({"_id": {"$in": answer_list}})


